#include "stdMyString.h"
#include <cstring>

//无参构造
stdMyString::stdMyString()
{   
    //std::cout<<"无参构造\n";
    this->size = 0;
    this->capacity = 16;
    this->s = new char[this->capacity];
    memset(s, 0, 15);
}

//带参构造
stdMyString::stdMyString(const char *str)
{
    //std::cout<<"带参构造\n";
    this->size = strlen(str);
    if (strlen(str) <=15)
    {
        this->capacity = 16;
        this->s = new char[this->capacity];
        memset(s, 0, 15);
        strcpy(this->s, str);
    }
    else
    {
        this->capacity = this->size + 1;
        this->s = new char[this->capacity];
        memset(s, 0, this->size);
        strcpy(this->s, str);        
    }

}

//拷贝构造
stdMyString::stdMyString(const stdMyString &str)
{
    //std::cout<<"拷贝构造\n";
    this->capacity = str.capacity;
    this->size = str.size;
    this->s = new char[this->capacity];
    memset(s, 0, this->capacity);
    strcpy(this->s, str.s);        
}

//析构函数
stdMyString::~stdMyString()
{
    //std::cout<<"析构函数\n";

    this->size = 0;
    this->capacity = 0;
    delete[]this->s;

}

stdMyString stdMyString::Number(int a)
{
    stdMyString s;
    sprintf(s.s, "%d", a);
    return s;
}

//运算符重载+
stdMyString stdMyString::operator+(const stdMyString &str)
{
    stdMyString result(*this);
    result.size = this->size + str.size;
    if (result.size < result.capacity)//不需要扩容
    {
        strcat(result.s, str.s);
    }
    else
    {
        result.capacity = result.size + 1;
        char * newStr = new char[result.capacity];
        strcpy(newStr, this->s);
        strcat(newStr, str.s);
        delete[]result.s;
        result.s = newStr;
    }
    return result;
}

//运算符重载-
stdMyString stdMyString::operator-(const stdMyString &str)
{
    stdMyString result(*this);
    char * deleteS = strstr(result.s, str.s);//字符串匹配函数
    while(deleteS != nullptr)
    {
        char * backS = deleteS + str.size;
        while(*backS != '\0')
            *deleteS++ = *backS++;
        *deleteS = '\0';
        deleteS = strstr(result.s, str.s);
    }

    return result;
}

//运算符重载+=
stdMyString& stdMyString::operator+=(const stdMyString &str)
{
    *this = *this + str;
    return * this;
}

//运算符重载+= 字符
stdMyString &stdMyString::operator+=(const char c)
{
    if (this->size + 1 == this->capacity)
    {
        this->capacity *= 2;
        char *newStr = new char[this->capacity];
        strcpy(newStr, this->s);
        delete []this->s;
        this->s = newStr;
    }
    this->s[this->size] = c;
    this->s[this->size + 1] = '\0';
    this->size++;
    return *this;
}

//运算符重载==
bool stdMyString::operator==(const stdMyString &str)
{
    
    return strcmp(this->s, str.s) == 0;
}

//运算符重载！=
bool stdMyString::operator!=(const stdMyString &str)
{
    return strcmp(this->s, str.s) != 0;
}

//运算符重载>
bool stdMyString::operator>(const stdMyString &str)
{
    return strcmp(this->s, str.s) > 0;
}

//[]运算符重载
char &stdMyString::operator[](int idx)
{
    return this->s[idx];
}

//int（）运算符重载
stdMyString::operator int()//强转
{
    return atoi(this->s);
}

//double()
stdMyString::operator double()
{
    return atof(this->s);
}

//字符串切割
myStringList stdMyString::split(const stdMyString &str)
{
    stdMyString temp(*this);
    myStringList list;
    char * s = strtok(temp.s, str.s);
    while (s != nullptr)
    {
        list += s;
        s = strtok(nullptr, str.s);
    }
    return list;
}

/*赋值运算符的重载 避免重复释放*/
stdMyString& stdMyString::operator=(const stdMyString &str)
{
    //std::cout<<"赋值运算符的重载\n";
    this->size = str.size;
    this->capacity = str.capacity;
    delete []this->s;
    this->s = new char(str.capacity);
    strcpy(this->s, str.s);
    return *this;//不加引用返回左值 发生拷贝
}

//<<输出流
std::ostream &operator<<(std::ostream &os,const stdMyString &str)
{
    //os<<"size:"<<str.size<<" ,capacity:"<<str.capacity<<std::endl;
    os<<str.s;
    return os;
}

//>>输入流
std::istream &operator>>(std::istream &is, stdMyString &str)
{
    //判断写入
    // char temp[1024] = {0};
    // is>>temp;
    // str = temp;
    char c = '\0';
    while((c = getchar()) != '\n')
    {
        str += c;
    }
    std::cout<<"size"<<str.size<<std::endl;
    return is;
}



//二维数组 构造
myStringList::myStringList()
{
    this->size = 0;
    this->capacity = 15;
    this->string = new stdMyString[this->capacity];
}

//二维数组 析构
myStringList::~myStringList()
{
    this->size = this->capacity = 0;
    delete []this->string;

}

//二维数组+=运算符重载
myStringList &myStringList::operator+=(const stdMyString &str)
{
    if (this->size == this->capacity)
    {
        this->capacity *= 2;
        stdMyString * newPtr = new stdMyString[this->capacity];
        for (int idx = 0; idx < this->size; idx++)
        {
            newPtr[idx] = this->string[idx];
        }
        delete []this->string;
        this->string = newPtr;
    }
    this->string[this->size++] = str;
    return * this;
}

myStringList &myStringList::operator-=(const stdMyString &str)
{
    for (int idx = 0; idx < this->size; idx++)
    {
        if(this->string[idx] == str)
        {
            removeByIndex(idx);
            idx--;

        }
    }
    return * this;
}

stdMyString &myStringList::operator[](int index)
{
    return this->string[index];
}

void myStringList::removeByIndex(int index)
{
    if (index < 0 ||index >= this->size)
        return ;
    for (int idx = index; idx < this->size - 1; idx++)
    {
        this->string[idx] = this->string[idx + 1];
    }
    this->size--;
}

std::ostream &operator<<(std::ostream &os, const myStringList &list)
{
    for (int i = 0; i < list.size; i++)
    {
        os<<list.string[i]<<std::endl;
    }
    return os;

}

